<?php 
namespace app\dboard\logic;
use think\Db;
use think\facade\Session;
class Shop extends Base
{

	function __construct()
	{
	}

	public function edit($param)
	{
		$name = isset($param['name']) ? trim($param['name']):'';
		$contacts = isset($param['contacts']) ? trim($param['contacts']):'';
		$phone = isset($param['phone']) ? trim($param['phone']):'';
		$province = isset($param['province']) ? trim($param['province']):'';
		$city = isset($param['city']) ? trim($param['city']):'';
		$area = isset($param['area']) ? trim($param['area']):'';
		$address = isset($param['address']) ? trim($param['address']):'';
		$description = isset($param['description']) ? trim($param['description']):'';
		$pic1 = isset($param['pic1']) ? trim($param['pic1']):'';
		$pic2 = isset($param['pic2']) ? trim($param['pic2']):'';
		$pic3 = isset($param['pic3']) ? trim($param['pic3']):'';
		$video1 = isset($param['video1']) ? trim($param['video1']):'';
		$video2 = isset($param['video2']) ? trim($param['video2']):'';
		$video3 = isset($param['video3']) ? trim($param['video3']):'';
		$vpic1 = isset($param['vpic1']) ? trim($param['vpic1']):'';
		$vpic2 = isset($param['vpic2']) ? trim($param['vpic2']):'';
		$vpic3 = isset($param['vpic3']) ? trim($param['vpic3']):'';
		$store_photos = isset($param['store_photos']) ? trim($param['store_photos']):'';
		$sid = isset($param['sid']) ? intval($param['sid']):0;
    if (!$sid || !$name || !$contacts || !$phone || !$address) {
      $this->res_data['Msg'] = "缺少必填信息";
      return $this->res_data;
    }
		$store= Db::name('store')->where([['id','=',$sid]])->find();
		if (!$store) {
			$this->res_data['Msg'] = "无此门店";
			return $this->res_data;
		}
    $data['name'] =$name;
    $data['contacts'] =$contacts;
    $data['phone'] =$phone;
    $data['province'] =$province;
    $data['city'] =$city;
    $data['area'] =$area;
    $data['address'] =$address;
    $data['description'] =$description;
    $data['pic1'] =$pic1;
    $data['pic2'] =$pic2;
    $data['pic3'] =$pic3;
    $data['video1'] =$video1;
    $data['video2'] =$video2;
    $data['video3'] =$video3;
    $data['vpic1'] =$vpic1;
    $data['vpic2'] =$vpic2;
    $data['vpic3'] =$vpic3;
    $data['store_photos'] =$store_photos;
    $data['updatetime'] =time();
    //Db::startTrans();
    $sdata = Db::name('store')->where([['id','=',$sid]])->data($data)->update();
    if(!$sdata) {
        //Db::rollback();
        $this->res_data['Msg'] = '编辑失败';
        return $this->res_data;
    }
    /*
    if($store_photos){
      Db::name('store_photo')->where([['sid','=',$sid]])->delete();
      $photos = explode(',',$store_photos);
      foreach($photos as $v){
         if(!$v) continue;
         $sp_data['sid'] = $sid; 
         $sp_data['path'] = $v; 
         $sp_insert = Db::name('store_photo')->insertGetId($sp_data);
         if(!$sp_insert ) {
            Db::rollback();
            $this->res_data['Msg'] = '编辑失败';
            return $this->res_data;
         }
      }
    }
    */
    //Db::commit();
    $this->res_data['Msg'] = '成功';
    $this->res_data['Code'] = 0;
    return $this->printResult($this->res_data);
	}
	public function editpwd($param)
	{
		$sid = isset($param['sid']) ? trim($param['sid']):'';
		$oldpwd = isset($param['oldpwd']) ? trim($param['oldpwd']):'';
		$newpwd = isset($param['newpwd']) ? trim($param['newpwd']):'';
		$newpwd_confirm = isset($param['newpwd_confirm']) ? trim($param['newpwd_confirm']):'';
    if (!$sid || !$oldpwd || !$newpwd || !$newpwd_confirm ) {
      $this->res_data['Msg'] = "缺少必填信息";
      return $this->res_data;
    }
    if ($newpwd != $newpwd_confirm ) {
      $this->res_data['Msg'] = "新密码两次输入不一致";
      return $this->res_data;
    }
    $password=get_admin_pwd($oldpwd);
		$store= Db::name('store')->where([['id','=',$sid],['password','=',$password]])->find();
		if (!$store) {
			$this->res_data['Msg'] = "旧密码错误";
			return $this->res_data;
		}
    
    $data['password'] =get_admin_pwd($newpwd);
    $data['updatetime'] =time();
    $data['token'] ='';
    $sdata = Db::name('store')->where([['id','=',$sid]])->data($data)->update();
    if(!$sdata) {
        //Db::rollback();
        $this->res_data['Msg'] = '编辑失败';
        return $this->res_data;
    }

    $this->res_data['Msg'] = '成功';
    $this->res_data['Code'] = 0;
    return $this->printResult($this->res_data);
	}
}

?>
